package com.alipay.app.groupmeal.standard.widget.business;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.Consumer;

/**
 * @ProjectName: 团餐
 * @ClassName: FakeToastVieW
 * @Description: java类作用描述
 * @Author: bd-001935
 * @CreateDate: 4/18/22 3:05 PM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/18/22 3:05 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class FakeToastView extends BaseBScreenView {

    private TextView mTvToast;
    private Disposable mdDisposable;

    public FakeToastView(Context context) {
        this(context, null);
    }

    public FakeToastView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FakeToastView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_fake_toast;
    }

    @Override
    protected void initView(View view) {
        mTvToast = view.findViewById(R.id.tv_toast);
    }

    @Override
    protected void initData() {

    }

    public void showToastAndInterval(String s) {
        if (TextUtils.isEmpty(s))
            return;
        post(new Runnable() {
            @Override
            public void run() {
                mTvToast.setText(s);
                setVisibility(VISIBLE);
                showInterval(3);
            }
        });
    }

    public void showToast(String s) {
        if (TextUtils.isEmpty(s))
            return;
        post(new Runnable() {
            @Override
            public void run() {
                mTvToast.setText(s);
                setVisibility(VISIBLE);
            }
        });
    }

    /**
     * 显示倒计时
     *
     * @param count
     */
    private void showInterval(int count) {
        mdDisposable = Flowable.intervalRange(0, count, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {

                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        setVisibility(GONE);
                    }
                })
                .subscribe();
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == GONE) {
            if (mdDisposable != null && !mdDisposable.isDisposed()) {
                mdDisposable.dispose();
                mdDisposable = null;
            }
        }
    }
}
